package org.jeecg.chase.common.script.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.chase.common.base.data.BaseQuery;
import org.jeecg.chase.common.base.data.CommonDetailQuery;
import org.jeecg.chase.common.script.service.CommonDataService;
import org.jeecg.chase.common.supper.scene.SceneContainer;
import org.jeecg.chase.common.utils.BeanUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author leikai
 * @version 1.0
 * @description:通用controller
 * @date 2021/07/23/0023 15:57
 */
@RestController
@RequestMapping("/chase/commonData")
public class CommonDataController {

    @Autowired
    private CommonDataService commonDataService;


    /**
     * 通用列表查询
     *
     * @param scene
     * @param param
     * @return org.jeecg.chase.common.result.ListResult
     * @author leikai
     * @date 2021/07/23/0023 16:03
     */
    @CrossOrigin(origins = "*")
    @PostMapping(value = "/{scene}/getList")
    public Result getList(@PathVariable("scene") String scene,
                          @RequestBody Map<String, Object> param) {
        Result<List<?>> result = new Result();
        try {
            Class<?> queryClass = SceneContainer.get("/" + scene + "/getList");
            BaseQuery baseQuery = (BaseQuery) BeanUtils.map2Bean(param, queryClass);
            result.setResult(commonDataService.getList(baseQuery));
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("查询失败");
        }
        return result;
    }

    @CrossOrigin(origins = "*")
    @PostMapping(value = "/{scene}/getPage")
    public Result getPage(@PathVariable("scene") String scene,
                          @RequestBody Map<String, Object> param) {
        Result<Page<?>> result = new Result<Page<?>>();
        try {
            Class<?> queryClass = SceneContainer.get("/" + scene + "/getPage");
            BaseQuery pageBaseQuery = (BaseQuery) BeanUtils.map2Bean(param, queryClass);

            Page<?> page = commonDataService.getPage(pageBaseQuery);
            result.setResult(page);
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("查询失败");
        }
        return result;
    }

    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/{scene}/get", method = RequestMethod.GET)
    @ResponseBody
    public Result get(
            @PathVariable("scene") String scene,
            @RequestParam(value = "id", required = true) String id) {
        Result<Object> result = new Result<Object>();
        try {
            Class<?> clazz = SceneContainer.get("/" + scene + "/get");
            Map<String, Object> param = new HashMap<String, Object>();
            CommonDetailQuery detailBaseQuery = (CommonDetailQuery) BeanUtils.map2Bean(param, clazz);
            detailBaseQuery.setId(id);
            Object object = commonDataService.get(detailBaseQuery);
            if (null != object) {
                result.setResult(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("查询失败");
        }
        return result;
    }

    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/{scene}/getMap", method = RequestMethod.GET)
    @ResponseBody
    public Result getMap(
            @PathVariable("scene") String scene) {
        Result<Object> result = new Result<Object>();
        try {
            Class<?> clazz = SceneContainer.get("/" + scene + "/getMap");
            Map<String, Object> param = new HashMap<String, Object>();
            CommonDetailQuery detailBaseQuery = (CommonDetailQuery) BeanUtils.map2Bean(param, clazz);
            Object object = commonDataService.get(detailBaseQuery);
            if (null != object) {
                result.setResult(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("查询失败");
        }
        return result;
    }
}
